﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Linq;
using System.Threading.Tasks;
using System.Windows.Forms;
using DevExpress.XtraEditors;
using DevExpress.XtraGrid.Views.Grid.ViewInfo;
using DevExpress.XtraGrid.Views.Grid;
using DevExpress.XtraGrid;
using LSQL;

namespace Shopfloormanagement.Forms
{
	public partial class messageForm: DevExpress.XtraEditors.XtraForm
	{
        public static messageForm instance = new messageForm();
        public messageForm()
		{
            instance = this;
            InitializeComponent();
		}

        private void InitGridView(GridView grid)
        {
            grid.Columns["ID"].Visible = false;
            grid.Columns["负责人姓名"].Visible = false;

            grid.Columns["DateTime"].Caption = "Date";
            grid.Columns["提出部门"].Caption = "Assign department";
            grid.Columns["TopicINFO"].Caption = "Topic";
            grid.Columns["TodoINFO"].Caption = "Todo/Action";
            grid.Columns["负责部门"].Caption = "Res. department";
            grid.Columns["负责人姓名"].Caption = "Res. person";

            grid.Columns["DateTime"].Width = 150;
            grid.Columns["RootCause"].Width = 200;
            grid.Columns["提出部门"].Width = 100;
            grid.Columns["TopicINFO"].Width = 400;
            grid.Columns["TodoINFO"].Width =470;
            grid.Columns["负责部门"].Width = 100;
            grid.Columns["DueDate"].Width = 100;
            grid.Columns["Status"].Width = 70;

            grid.Columns["DateTime"].OptionsColumn.AllowEdit = false;
            grid.Columns["提出部门"].OptionsColumn.AllowEdit = false;
            grid.Columns["TopicINFO"].OptionsColumn.AllowEdit = false;
            grid.Columns["TodoINFO"].OptionsColumn.AllowEdit = false;
            grid.Columns["负责部门"].OptionsColumn.AllowEdit = false;
            grid.Columns["DueDate"].OptionsColumn.AllowEdit = false;

            // 允许行高自动调整
            grid.OptionsView.RowAutoHeight = true;

            // 让所有行都能换行
            grid.Appearance.Row.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            // 让特定列换行
            grid.Columns["TopicINFO"].AppearanceCell.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            grid.Columns["RootCause"].AppearanceCell.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            grid.Columns["TodoINFO"].AppearanceCell.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;
            grid.Columns["负责部门"].AppearanceCell.TextOptions.WordWrap = DevExpress.Utils.WordWrap.Wrap;

            // 让单元格可以输入多行文本
            var memoEdit = new DevExpress.XtraEditors.Repository.RepositoryItemMemoEdit();
            grid.Columns["TopicINFO"].ColumnEdit = memoEdit;
            grid.Columns["RootCause"].ColumnEdit = memoEdit;
            grid.Columns["TodoINFO"].ColumnEdit = memoEdit;
            grid.Columns["负责部门"].ColumnEdit = memoEdit;

            // 创建 ImageComboBoxEdit
            var repositoryItemImageComboBox = new DevExpress.XtraEditors.Repository.RepositoryItemImageComboBox();

            // 添加预设图片
            ImageList imageList = new ImageList();
            imageList.Images.Add("image3", Properties.Resources.进度0);
            imageList.Images.Add("image3", Properties.Resources.进度25);
            imageList.Images.Add("image3", Properties.Resources.进度50);
            imageList.Images.Add("image3", Properties.Resources.进度75);
            imageList.Images.Add("image3", Properties.Resources.进度100);

            // 绑定图片到 ImageComboBoxEdit
            repositoryItemImageComboBox.SmallImages = imageList;
            repositoryItemImageComboBox.Items.Add(new DevExpress.XtraEditors.Controls.ImageComboBoxItem("", 1, 0));
            repositoryItemImageComboBox.Items.Add(new DevExpress.XtraEditors.Controls.ImageComboBoxItem("", 2, 1));
            repositoryItemImageComboBox.Items.Add(new DevExpress.XtraEditors.Controls.ImageComboBoxItem("", 3, 2));
            repositoryItemImageComboBox.Items.Add(new DevExpress.XtraEditors.Controls.ImageComboBoxItem("", 4, 3));
            repositoryItemImageComboBox.Items.Add(new DevExpress.XtraEditors.Controls.ImageComboBoxItem("", 5, 4));


            grid.Columns["Status"].ColumnEdit = repositoryItemImageComboBox;
        }

        private void messageForm_Load(object sender, EventArgs e)
        {
            lbUserName.Text = SystemInfo.userName;
            DataTable dtMessage = publicFunc.UserIncompleteAction(SystemInfo.userID);
            gcTask.DataSource = dtMessage;

            DataTable dtUnconfirm = publicFunc.UserUnconfirmAction(SystemInfo.userID);
            gcTodo.DataSource = dtUnconfirm;

            InitGridView(gvTask);
            InitGridView(gvTodo);
        }

        private int todoIndex = -1;
        private int unconIndex = -1;

        private void gvTodo_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // 获取鼠标点击的 GridView 位置
                GridView view = sender as GridView;
                GridHitInfo hitInfo = view.CalcHitInfo(e.Location);

                if (hitInfo.InRow || hitInfo.InRowCell) // 确保右键点击在行内
                {
                    unconIndex = hitInfo.RowHandle;
                    popUnconfirm.ShowPopup(Control.MousePosition); // 在鼠标位置显示菜单
                }
            }
        }

        private void gvTask_MouseUp(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                // 获取鼠标点击的 GridView 位置
                GridView view = sender as GridView;
                GridHitInfo hitInfo = view.CalcHitInfo(e.Location);

                if (hitInfo.InRow || hitInfo.InRowCell) // 确保右键点击在行内
                {
                    todoIndex = hitInfo.RowHandle;
                    popTodo.ShowPopup(Control.MousePosition); // 在鼠标位置显示菜单
                }
            }
        }

        private bool isTaskModified;
        private void gvTask_CellValueChanged(object sender, DevExpress.XtraGrid.Views.Base.CellValueChangedEventArgs e)
        {
            if (e.Column.FieldName == "DueDate" || e.Column.FieldName == "DateTime" || e.Column.FieldName == "Status")
            {
                isTaskModified = true; // 记录修改状态

                // 弹出提示框提醒用户保存
                DialogResult res = XtraMessageBox.Show("数据已修改，是否保存！", "提示", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
                if (res == DialogResult.Yes)
                {
                    gvTask.CloseEditor();  // 确保 GridView 结束编辑状态
                    gvTask.UpdateCurrentRow();

                    DataTable dt = (DataTable)gcTask.DataSource; // 获取绑定的数据

                    foreach (DataRow row in dt.Rows)
                    {
                        if (row.RowState == DataRowState.Modified) // 只更新被修改的行
                        {
                            string id = row["ID"].ToString();
                            string dueDate = row["DueDate"].ToString();
                            string statusPic = row["Status"].ToString();

                            bool update = SQL.Update(SystemInfo.SQL, "Todo", $"DueDate = '{dueDate}',Status = '{statusPic}'", $"ID = {id}");

                            if (update)
                            {
                                XtraMessageBox.Show("数据已成功保存！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                                InitGridView(gvTask);
                            }
                        }
                    }

                }
            }
        }

       

        private void barButtonItem2_ItemClick(object sender, DevExpress.XtraBars.ItemClickEventArgs e)
        {
            if (unconIndex != -1)
            {
                int confirmID = (int)gvTodo.GetRowCellValue(unconIndex, "ID");
                bool res = SQL.Update(SystemInfo.SQL, "Todo", "isConfirmation = 1", $"ID = {confirmID}");
                if (res)
                {
                    XtraMessageBox.Show("成功关闭！", "成功", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    InitGridView(gvTodo);
                }
            }
        }
    }
}